/**
 * 权重模型threadModel.js
 * 版本号： v1.0.0
 */
$(function(){
    $("#threadModel_dataList").datagrid({
        fit:true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        border:false,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/threadModel/threadModelList.do',
        idField: 'id',
        striped:true,
        frozenColumns:[[
            {field:'id',checkbox:true}
        ]],
        columns: [
            [
                {field: 'key', title: '应用KEY', width: 85, align: 'center', fixed: true},
                {field: 'value', title: '线程数量', width: 70,align: 'center', fixed: true},
                {field: 'type', title: '线程类型', width: 70, align: 'center', fixed: true},
                {field: 'createDate', title: '创建时间', fit: true, width: 130}
            ]
        ],
        onSelect:function(rowIndex, rowData){
        },
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                $("#threadModel_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#threadModel_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            threadModel_add();
                        }
                    });
                }
                //删除按钮
                if("delBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            threadModel_del();
                        }
                    });
                }
                //修改按钮
                if("editBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            threadModel_edit();
                        }
                    });
                }
                //详情按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            threadModel_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
                //重置按钮
                if("resetBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            threadModel_resetKey();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 添加权重模型
 */
function threadModel_add(){
    $("#threadModel_dialog").dialog({
        title: '添加权重',
        width: 420,
        modal: true,
        closed: true,
        height: 230,
        iconCls:"icon-custom-add",
        buttons:[{
            text:"添加",
            iconCls:"icon-role-state-ok",
            handler:function(){
                doOptionThreadModel();
            }
        },{
            text:"取消",
            iconCls:"icon-role-state-no",
            handler:function(){
                $('#threadModel_dialog').dialog('close');
            }
        }]
    });
    $("#id").val("");
    $("#key").textbox('setValue',"");
    $("#value").textbox('setValue',"");
    $("#threadModel_dialog").dialog("move",{left:450,top:100});
    $("#threadModel_dialog").dialog('open');

}

/**
 * 删除权重模型
 */
function threadModel_del() {
    var row = $("#threadModel_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择删除权重模型", timeout: 3000, showType: "slide" });
        return;
    }
    $.messager.confirm('删除提示', '您确定要删除 ' + row.key + ' 权重模型吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/threadModel/del.do?id=" + row.id,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#threadModel_dataList").datagrid("reload", {});
                }
            })
        }
    });
}

/**
 * 修改权重模型
 * @returns {boolean}
 */
function threadModel_edit(){
    var row = $("#threadModel_dataList").datagrid("getSelected");
    if(row == null){
        $.messager.show({ title:'操作提示', msg:"请选择修改权重模型", timeout:3000, showType:"slide" });
        return;
    }
    $("#threadModel_dialog").dialog({
        title: '修改权重',
        width: 420,
        modal: true,
        closed: true,
        height: 230,
        iconCls:"icon-custom-edit",
        buttons:[{
            text:"修改",
            iconCls:"icon-role-state-ok",
            handler:function(){
                doOptionThreadModel();
            }
        },{
            text:"取消",
            iconCls:"icon-role-state-no",
            handler:function(){
                $('#threadModel_dialog').dialog('close');
            }
        }]
    });

    $('#threadModel_dialog').dialog('open');
    $("#threadModel_dialog").dialog("move",{left:450,top:100});
    $("#threadModelForm").form("load", row);
}


/**
 * 刷新权重
 */
function threadModel_view(iconCls){
    var row = $("#threadModel_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的权重详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(qzxq.id, qzxq.url + row.id, iconCls, qzxq.closable, qzxq.title);
}





/**
 * 执行保存操作方法
 */
function doOptionThreadModel(){
    var params = $("#threadModelForm").serialize();
    $("#threadModelForm").form("submit",{
        cache:false,
        data: params,
        url: CONTEXT_PATH + "/threadModel/save.do",
        onSubmit:function() {
            return $(this).form("validate");
        },
        success:function(data) {
            var obj = JSON.parse(data);
            messager(obj);
            $('#threadModel_dialog').dialog('close');
            $("#threadModel_dataList").datagrid("reload",{});
        }
    });
}

/**
 * 重置权重模型
 */
function threadModel_resetKey(){
    $.ajax({
        url: CONTEXT_PATH + "/threadModel/resetThreadModel.do",
        type:"get",
        success:function(data){
            messager(data);
        }
    })

}